﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Backend.JYD.Model.Cust;
using Backend.JYD.Model;
using System.Data.Entity.Validation;
using Backend.JYD.Common;

namespace Backend.JYD.EFDAL.DAL
{
    public class CustMarketingInfoDAL : IDAL.Cust.ICustMarketingInfo
    {
        public long AddCustMarketingInfo(CustMarketingInfo info)
        {
            using (JydDataContext db = new JydDataContext())
            {
                try
                {
                    db.CustMarketingInfoes.Add(info);
                    db.SaveChanges();
                    return info.MarketingId;
                }
                catch (DbEntityValidationException ex)
                {
                    LogHelper.Log(ex);
                    return 0;
                }
            }
        }

        public bool UpdateCustMarketingInfo(CustMarketingInfo info)
        {
            using (JydDataContext db = new JydDataContext())
            {
                try
                {
                    var query = db.CustMarketingInfoes.Where(p => p.MarketingId == info.MarketingId);
                    if (query != null)
                    {
                        var temp = query.FirstOrDefault();
                        if (temp != null)
                        {
                            if (temp.Status == 1 && info.Status == 1)
                            {
                                temp.ScheduleTime = info.ScheduleTime;
                                temp.ValidDays = info.ValidDays;
                                temp.Memo = info.Memo;
                            }
                            else if (temp.Status == 1 && (info.Status == 2 || info.Status == 4))
                            {
                                temp.Status = info.Status;
                                temp.Remark = info.Remark;
                                temp.TouchTime = info.TouchTime;
                                temp.Ways = info.Ways;
                            }
                            else if (temp.Status == 1 && info.Status == 5)
                            {
                                temp.Status = info.Status;
                                temp.Remark = info.Remark;
                            }
                            //else if (temp.Status == 2 && info.Status == temp.Status)
                            //{
                            //    temp.Remark = info.Remark;
                            //    temp.TouchTime = info.TouchTime;
                            //    temp.Ways = info.Ways;
                            //}

                            temp.OpId = info.OpId;
                            temp.UpdateTime = DateTime.Now;

                            return db.SaveChanges() > 0;
                        }
                    }
                    return false;
                }
                catch (DbEntityValidationException ex)
                {
                    LogHelper.Log(ex);
                    return false;
                }
            }
        }
        public bool CompleteCustMarketingInfo(CustMarketingInfo info)
        {
            using (JydDataContext db = new JydDataContext())
            {
                try
                {
                    var query = db.CustMarketingInfoes.Where(p => p.MarketingId == info.MarketingId);
                    if (query != null)
                    {
                        var temp = query.FirstOrDefault();
                        if (temp != null)
                        {
                            temp.UpdateTime = DateTime.Now;
                            temp.Status = info.Status;
                            temp.TouchTime = info.TouchTime;
                            temp.Remark = info.Remark;

                            return db.SaveChanges() > 0;
                        }
                    }
                    return false;
                }
                catch (DbEntityValidationException ex)
                {
                    LogHelper.Log(ex);
                    return false;
                }
            }
        }


        /// <summary>
        /// 查询数据
        /// </summary>
        public CustMarketingInfo GetCustMarketingInfoByMarketingId(Int64 empId, Int64 marketingId)
        {
            using (JydDataContext db = new JydDataContext())
            {
                try
                {
                    IQueryable<CustMarketingInfo> query = db.CustMarketingInfoes.Where(p => p.OpId == empId && p.MarketingId == marketingId);
                    return query.FirstOrDefault();
                }
                catch (DbEntityValidationException ex)
                {
                    LogHelper.Log(ex);
                    return null;
                }
            }
        }

        public IList<CustMarketingInfo> GetCustMarketingInfoByEmpId(Int64 empId)
        {
            using (JydDataContext db = new JydDataContext())
            {
                try
                {
                    var query = db.CustMarketingInfoes.Where(p => p.OpId == empId);
                    return query == null ? null : query.ToList();
                }
                catch (DbEntityValidationException ex)
                {
                    LogHelper.Log(ex);
                    return null;
                }
            }
        }

        public PageInfo<CustMarketingInfo> GetCustMarketingInfoByPage(Int64? empId, DateTime? follow, int? status, PageInfo page)
        {
            using (JydDataContext db = new JydDataContext())
            {
                IQueryable<CustMarketingInfo> query = db.CustMarketingInfoes;
                if (empId.HasValue)
                    query = query.Where(p => p.OpId == empId);
                if (status.HasValue && status > 0)
                    query = query.Where(p => p.Status == status);
                if (follow.HasValue)
                    query = query.Where(p => p.ScheduleTime >= follow && p.ScheduleTime <= follow.Value.AddDays(p.ValidDays ?? 0));
                query = query.OrderBy(p => p.Status).ThenByDescending(p => p.TouchTime);
                return new PageInfo<CustMarketingInfo>(page, query);
            }
        }

        public PageInfo<CustMarketingInfo> GetCustMarketingInfoByPage(Int64? empId, DateTime? start, DateTime? end, int? status, PageInfo page)
        {
            using (JydDataContext db = new JydDataContext())
            {
                IQueryable<CustMarketingInfo> query = db.CustMarketingInfoes;
                if (empId.HasValue)
                    query = query.Where(p => p.OpId == empId);
                if (status.HasValue && status > 0)
                    query = query.Where(p => p.Status == status);
                if (start.HasValue)
                    query = query.Where(p => p.CreateTime >= start);
                if (end.HasValue)
                    query = query.Where(p => p.CreateTime <= end);
                query = query.OrderBy(p => p.Status).ThenByDescending(p => p.TouchTime);
                return new PageInfo<CustMarketingInfo>(page, query);
            }
        }

        public IList<CustMarketingInfo> GetCustMarketingInfoByTaskDetailIds(List<Int64> taskDetailIds)
        {
            Int64[] ids = taskDetailIds.ToArray();
            using (JydDataContext db = new JydDataContext())
            {
                IQueryable<CustMarketingInfo> query = db.CustMarketingInfoes.Where(p => ids.Contains(p.TaskDetailId));
                return query == null ? null : query.ToList();
            }
        }

        public CustMarketingInfo GetCustMarketingInfoByTaskDetailId(Int64? taskId, Int64 taskDetailId)
        {
            using (JydDataContext db = new JydDataContext())
            {
                if (taskId.HasValue)
                    return db.CustMarketingInfoes.Where(p => p.TaskId == taskId && p.TaskDetailId == taskDetailId).FirstOrDefault();
                else
                    return db.CustMarketingInfoes.Where(p => p.TaskDetailId == taskDetailId).FirstOrDefault();
            }
        }

        public PageInfo<CustBase> SearchCustBaseInfo(PageInfo page, string name, string phone)
        {
            using (JydDataContext db = new JydDataContext())
            {
                IQueryable<CustMarketingInfo> query = db.CustMarketingInfoes.Where(p => p.CustNo == null || p.CustNo == string.Empty);
                if (!string.IsNullOrWhiteSpace(name))
                {
                    query = query.Where(p => p.CustName.Contains(name));
                }
                if (!string.IsNullOrWhiteSpace(phone))
                {
                    query = query.Where(p => p.Phone == phone);
                }
                //其他条件代码
                var temp = new PageInfo<CustMarketingInfo>(page, query);
                if (temp == null || temp.PageList == null)
                {
                    return new PageInfo<CustBase>(page);
                }
                return temp.CastHandle<CustBase>(ConvertCustMarketingInfo);
            }
        }

        private CustBase ConvertCustMarketingInfo(CustMarketingInfo info)
        {
            if (info == null) return null;

            CustBase cust = new CustBase();
            cust.CustName = info.CustName;
            cust.CustNo = info.CustNo;
            cust.PaperNo = info.PaperNo;
            cust.PaperType = info.PaperType;
            cust.Phone = info.Phone;
            return cust;
        }
    }
}
